package com.dong.internetWorm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dong.common.collect.ListUtils;
import com.dong.internetWorm.mapper.MovieMapper;
import com.dong.internetWorm.model.Movie;
import com.dong.internetWorm.util.GetJson;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        String resource = "mybatis-config.xml";// 定义配置文件路径
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);// 读取配置文件
        } catch (IOException e) {
            e.printStackTrace();
        }

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);// 注册mybatis工厂
        SqlSession sqlSession = sqlSessionFactory.openSession();// 得到连接对象
        MovieMapper movieMapper = sqlSession.getMapper(MovieMapper.class);// 从mybatis中得到dao对象

        int start = 9020;// 当前到第多少条
        int total = 0;// 记录数
        while (true) {
            try {
                String address = "https://Movie.douban.com/j/new_search_subjects?sort=U&range=0,10&tags=&start=" + start;
                JSONObject dayLine = new GetJson().getHttpJson(address, 1);
                JSONArray json = dayLine.getJSONArray("data");
                List<Movie> list = JSON.parseArray(json.toString(), Movie.class);
                if (ListUtils.isEmpty(list)) {
                    System.out.println("已经爬取到底了");
                    sqlSession.close();
                    break;
                }
                for (Movie movie : list) {
                    Movie dbMovie = movieMapper.findById(movie.getId());
                    if (dbMovie == null) {
                        movieMapper.insert(movie);
                    } else {
                        movieMapper.update(movie);
                    }
                    sqlSession.commit();
                }
                total += list.size();
                System.out.println("正在爬取中---共抓取:" + total + "条数据");
                start += 20;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}